<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$store_list = pdo_getall('yzzc_sun_store',['uniacid'=>$_W['uniacid']],['store_id','name']);
$distribution_list = pdo_getall('yzzc_sun_distribution_promoter',['uniacid'=>$_W['uniacid'],'status' => 1],['uid','name']);
$shuttle_list = pdo_getall('yzzc_sun_shuttle',['uniacid'=>$_W['uniacid']],['id','name']);
$where = "where i.store_id > 0 and  i.uniacid = " . $_W['uniacid'];
$keywords = $_GPC['keywords'] ?? '';
if ($keywords) {
    $where .= " and (o.username LIKE  '%$keywords%' or o.tel LIKE  '%$keywords%' or o.ordernum LIKE  '%$keywords%')";
}

$store_id = $_GPC['store_id'] ?? '';
if ($store_id){
    $where.= " and s.store_id=".$store_id;
}

$range_time = $_GPC['range_time'] ?? [];
$start_time = $end_time = '';
if ($range_time['start'] && $range_time['end']) {
    $start_time = $range_time['start'];
    $end_time = $range_time['end'];
    $end_time = strtotime($end_time);
    $start_time = strtotime($start_time);
    $where.=' and i.createtime > '.$start_time .' and i.createtime<='.$end_time;
}

$distribution_id = $_GPC['distribution_id'] ?? '';
if ($distribution_id){
    $where.=' and o.distribution_id = '.$distribution_id;
}

$shuttle_id = $_GPC['shuttle_id'] ?? '';
if ($shuttle_id){
    $where.=' and o.shuttle_id = '.$shuttle_id;
}

$page = max(1, intval($_GPC['page']));
$size = intval($_GPC['psize']) ? intval($_GPC['psize']) : 10;
$sql = 'SELECT i.id,i.total,i.branch_cost,i.branch_id,i.amount,o.shuttle_id,o.distribution_id,o.car_name,o.total_money,o.carnum,i.status,i.createtime,i.income_time,s.name as store_name,o.ordernum,o.username,o.tel FROM ' . tablename('yzzc_sun_store_income') . " i left join ".tablename('yzzc_sun_order')." o on i.order_id=o.id left join ".tablename('yzzc_sun_store')." s on i.store_id=s.store_id  {$where} ORDER BY i.createtime DESC LIMIT " . (($page - 1) * $size) . ',' . $size;

$info = pdo_fetchall($sql);

$search_total = $wait_amount = $finish_amount =  0;
if ($info){
    foreach ($info as $key => $value){
        $distribution_name = $shuttle_name = '';
        if ($value['distribution_id']){
            $distribution_name = pdo_fetchcolumn('select name from '.tablename('yzzc_sun_distribution_promoter').' where uid = :id', ['id' => $value['distribution_id']]);
        }

        if ($value['shuttle_id']){
            $shuttle_name = pdo_fetchcolumn('select name from '.tablename('yzzc_sun_shuttle').' where id = :id', ['id' => $value['shuttle_id']]);
        }
        $info[$key]['distribution_name'] = $distribution_name;
        $info[$key]['shuttle_name'] = $shuttle_name;

        $info[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
        $info[$key]['income_time'] = $value['income_time'] ? date('Y-m-d H:i:s',$value['income_time']) : '';


    }

    //统计
    $sql = 'SELECT i.id,i.total,i.branch_cost,i.branch_id,i.amount,o.shuttle_id,o.distribution_id,o.car_name,o.total_money,o.carnum,i.status,i.createtime,i.income_time,s.name as store_name,o.ordernum,o.username,o.tel FROM ' . tablename('yzzc_sun_store_income') . " i left join ".tablename('yzzc_sun_order')." o on i.order_id=o.id left join ".tablename('yzzc_sun_store')." s on i.store_id=s.store_id  {$where} ";
    $total_count = pdo_fetchall($sql);

    foreach ($total_count as $value){
        if ($value['status'] == 0){
            $wait_amount+=$value['amount'];
        }
        if ($value['status'] == 1){
            $finish_amount+=$value['amount'];
        }
    }
}
$total = pdo_fetchcolumn("select count(*) from " . tablename("yzzc_sun_branch_income") . " where uniacid =" . $_W['uniacid']);
$pager = pagination($total, $page, $size);

if ($_GPC['op'] == 'delete') {
    $is = pdo_get('yzzc_sun_goods', array('sid' => $_GPC['id']));

    if ($is) {
        message('请先至车辆列表下架所有车辆才能删除', '', 'error');
    } else {
        $branch_info = pdo_get('yzzc_sun_branch', array('id' => $_GPC['id']));
        $res = pdo_delete('yzzc_sun_branch', array('id' => $_GPC['id']));
        if ($res) {
            if ($branch_info['type'] != 1){
                pdo_update('yzzc_sun_store',['status'=>0],['store_id'=>$branch_info['store_id']]);
            }
            message('操作成功', $this->createWebUrl('branchslist', array()), 'success');
        } else {
            message('操作失败', '', 'error');
        }
    }
}

if ($_GPC['op'] == 'income'){
    $branch_id = $_GPC['id'];
    $range_time = $_GPC['range_time'] ?? [];
    $where = 'i.status= 1 and i.branch_id='.$branch_id;
    $start_time = $end_time = '';
    if ($range_time){
        $start_time = $range_time['start'];
        $end_time = $range_time['end'];
        $end_time = strtotime($end_time);
        $start_time = strtotime($start_time);
        $where.=' and i.createtime > '.$start_time .' and i.createtime<='.$end_time;
    }

    $branch_info = pdo_get('yzzc_sun_branch',['uniacid'=>$_W['uniacid'],'id'=>$branch_id],['id','name']);
    $sql = 'SELECT i.id,i.total,i.type,i.platform_fee,i.branch_id,i.amount,i.status,i.createtime,o.ordernum,o.username,o.tel FROM ' . tablename('yzzc_sun_branch_income') . " i left join ".tablename('yzzc_sun_order')." o on i.order_id=o.id where  {$where} ORDER BY i.createtime DESC";

    $info = pdo_fetchall($sql);
    if ($info){
        foreach ($info as $key => $value){
            foreach ($branch_list as $val){
                if ($value['branch_id'] == $val['id']){
                    $info[$key]['branch_name'] = $val['name'];
                }
            }
            $info[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
        }
    }
    include $this->template('web/branch_store_income');
    return;
}
if ($_GPC['op'] == 'branch_income'){
    $branch_id = $_GPC['branch_id'];
    $range_time = $_GPC['range_time'] ?? [];
    $where = 'status= 1 and branch_id='.$branch_id;
    $type = $_GPC['type'];
    if ($type !== 'all'){
        $start_time = $_GPC['start_time'];
        $end_time = $_GPC['end_time'];
        $end_time = strtotime($end_time);
        $start_time = strtotime($start_time);
        $where.=' and createtime > '.$start_time .' and createtime<='.$end_time;
    }
    $branch_info = pdo_get('yzzc_sun_branch',['uniacid'=>$_W['uniacid'],'id'=>$branch_id],['id','name','balance']);
    $sql = 'SELECT * FROM ' . tablename('yzzc_sun_branch_income') . " where {$where}";

    $info = pdo_fetchall($sql);
    if (!$info){
        message('暂无结算收益','','error');
    }
    $price = 0;
    foreach ($info as $value){
        $price+=$value['amount'];
    }

    $branch_update = pdo_update('yzzc_sun_branch',['balance' => $branch_info['balance'] + $price],['uniacid'=>$_W['uniacid'],'id'=>$branch_id]);
    $update_sql = 'UPDATE ' . tablename('yzzc_sun_branch_income') . " set status= 2,income_time = ".time()." where {$where}";
    $income_update = pdo_query($update_sql);
    if ($branch_update && $income_update){
        message('结算成功','','success');
    }
    message('结算失败，请联系管理员','','error');
}
if ($_GPC['op'] == 'change') {
    if ($_GPC['states'] == 2) {
        $is = pdo_get('yzzc_sun_goods', array('sid' => $_GPC['id'], 'status' => 1));
        if ($is) {
            message('请先至车辆列表下架所有车辆才能停业', '', 'error');
        }
    }

    $res = pdo_update('yzzc_sun_branch', array('status' => $_GPC['states']), array('id' => $_GPC['id']));
    if ($res) {
        message('操作成功', $this->createWebUrl('branchslist', array()), 'success');
    } else {
        message('操作失败', '', 'error');
    }


}

include $this->template('web/store/store_income');